//
//  ChangeDPMessageVc.swift
//  Pods
//
//  Created by eme on 2017/4/6.
//
//
/*
 owner:gyh
 update:2017年04月06日09:47:52
 info: 修改店铺信息
 */
import UIKit
import UtilCore
import RxSwift
import CommonCore
import RickonCore
import BrandonCore


class ChangeDPMessageVc: Base_Vc {
    
    /****************************Storyboard UI设置区域****************************/
    @IBOutlet weak var message_TF: UITextField!
    
    /*----------------------------   自定义属性区域    ----------------------------*/
    var customTitle:String?                                 ///标题
    
    var buttonWithEditor:UIButton?                          ///保存按钮
    
    var spname:String?                                      ///简称
    
    var address:String?                                     ///详细地址
    
    var smvm:changeStoreMessageVm?                          ///修改店铺信息vm
    
    var spcontact:String?                                   ///联系人
    
    var CustomTextView = PlaceholderTextView()              ///详细地址
    
    /****************************Storyboard 绑定方法区域****************************/
    
    
    
    /**************************** 以下是方法区域 ****************************/
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = customTitle
        if title == "修改简称"{
            if spname == ""{
                
                self.message_TF.placeholder = "请输入简称"
            }else{
                
                self.message_TF.text = spname
            }
        }else if title == "请输入详细地址"{
            
            self.message_TF.isHidden = true
            self.CustomTextView.placeholderLabel.font = UIFont.init(name: "Helvetica Neue", size: 15)
            self.CustomTextView.placeholderLabel.text = "请输入详细地址"
            self.view.addSubview(self.CustomTextView)
            self.CustomTextView.snp.makeConstraints({ (make) in
                make.left.right.equalTo(0)
                make.top.equalTo(64)
                make.height.equalTo(80)
            })
            if address == ""{
                
                self.CustomTextView.placeholderLabel.text = "请输入详细地址"
                
            }else{
                
                self.CustomTextView.text = address
            }
            
        }else{
            if spcontact == ""{
                self.message_TF.placeholder = "请输入联系人姓名"
            }else{
                self.message_TF.text = spcontact
            }
        }
        
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        
    }
    /**
     界面基础设置
     */
    override func setupUI() {
        /**
         *  自定义 导航栏左侧 返回按钮
         */
        self.customLeftBarButtonItem()
        self.setTheEditorButton()
    }
    func setTheEditorButton(){
        
        buttonWithEditor = UIButton(type:.custom)
        buttonWithEditor?.frame = CGRect(x:0,y:0,width: 30,height: 15)
        buttonWithEditor?.titleLabel?.font = UIFont.init(name: "Helvetica Neue", size: 15)
        buttonWithEditor?.setTitle("保存", for: .normal)
        buttonWithEditor?.setTitleColor(UIColor.black, for: .normal)
        
        let editorButton = UIBarButtonItem(customView: buttonWithEditor!)
        self.navigationItem.rightBarButtonItem = editorButton
        
    }
    
    /**
     绑定到viewmodel 设置
     */
    override func bindToViewModel(){
        
        self.smvm = changeStoreMessageVm()
        
        smvm?.backMessage.subscribe(onNext:{ data in
            if data != false{
                
                self.view.toastCompletion("修改成功", completion: { _ in

                    _ = self.navigationController?.popViewController(animated: true)
                })
            }
        }).addDisposableTo(disposeBag)
        
        self.smvm?.error.bindTo(self.view.rx_error).addDisposableTo(disposeBag)
        smvm?.error.subscribe(onNext:{error in
            
            self.buttonWithEditor?.isEnabled = true
            
            }).addDisposableTo(disposeBag)
        
        
        buttonWithEditor?.rx.tap.subscribe(onNext:{ _ in
            
            self.view.endEditing(true)
            
            self.buttonWithEditor?.isEnabled = false
            
            if let model = AppData.loginedUser{
                if self.title == "修改简称"{
                    if (self.message_TF.text?.characters.count)! <= 26 && (self.message_TF.text?.characters.count)! >= 4{
                        self.smvm?.changeMessage.onNext(JSONHelper.parseToJson(["spname":self.message_TF.text!,"spid":model.thrid] as AnyObject) as String)
                    }else{
                        self.view.toastErrorCode(100504000)
                        //                    showMsg(100504000)
                        //                    self.view.makeToast((UtilCore.alertmsg[100504000]?.msgtitle)!)
                        self.buttonWithEditor?.isEnabled = true
                    }
                    
                    
                }else if self.title == "修改联系人姓名"{
                    if (self.message_TF.text?.characters.count)!  <= 15 && (self.message_TF.text?.characters.count)! > 0{
                        
                        self.smvm?.changeMessage.onNext(JSONHelper.parseToJson(["spcontact":self.message_TF.text!,"spid":model.thrid] as AnyObject) as String)
                    }else{
                        //                    self.view.makeToast((UtilCore.alertmsg[100504001]?.msgtitle)!)
                        self.view.toastErrorCode(100504001)
                        self.buttonWithEditor?.isEnabled = true
                    }
                }else{
                    if (self.CustomTextView.text?.characters.count)! >= 5 && (self.CustomTextView.text?.characters.count)! <= 120{
                        let commit = ["spaddress":self.CustomTextView.text!,"spid":model.thrid!]
                        self.smvm?.changeMessage.onNext(JSONHelper.parseToJson(commit as AnyObject) as String)
                    }else{
                        
                        self.view.toastErrorCode(100504002)
                        self.buttonWithEditor?.isEnabled = true
                    }
                }
            }
        }).addDisposableTo(disposeBag)
    }
}
